'''

'''


import struct
from data_type_yizimi import *
import os

def get_string_mode():
    string_mode = msg_mode(0x00000000, "string")
    string_mode.add_mode_seg(0, "data")
    return string_mode

def get_pcapheader_mode():
    Pcapheader_mode = msg_mode(0xFFFFFFFF, "Pcapheader")
    Pcapheader_mode.add_mode_seg(4, 'Magic')
    Pcapheader_mode.add_mode_seg(2, 'Major')
    Pcapheader_mode.add_mode_seg(2, 'Minor')
    Pcapheader_mode.add_mode_seg(4, 'ThisZone')
    Pcapheader_mode.add_mode_seg(4, 'SigFigs')
    Pcapheader_mode.add_mode_seg(4, 'SnapLen')
    Pcapheader_mode.add_mode_seg(4, 'LinkType')
    return Pcapheader_mode

def get_packetheader_mode():
    Packetheader_mode = msg_mode(0xFFFFFFFE, "Packetheader")
    Packetheader_mode.add_mode_seg(4, "ts_sec")
    Packetheader_mode.add_mode_seg(4, "ts_usec")
    Packetheader_mode.add_mode_seg(4, "incl_len")
    Packetheader_mode.add_mode_seg(4, "orig_len")
    Packetheader_mode.add_mode_seg(-1, "data")
    Packetheader_mode.set_data_name("incl_len")
    return Packetheader_mode


def read_packetheader(msg):
    mode = get_packetheader_mode()
    packetheader, msg = Matching_message(msg, [mode])
    packetheader.Print()
    return packetheader.msg_len, msg


def read_bin(length: int, data: bytes):
    # f = open("openflow6.pcap", "rb")
    int_size = struct.calcsize("c")
    get_bin = []
    # print(int_size)
    for i in range(int(length / int_size)):
        # print(data[:int_size])
        (i,), data = struct.unpack("c", data[:int_size]), data[int_size:]
        # print(hex(ord(i)), data)
        get_bin.append(ord(i))
    return get_bin

    
            
if __name__ == "__main__":
    print(os.path.getsize("./openflow6.pcap"))
    length = os.path.getsize("./openflow6.pcap")
    f = open("openflow6.pcap", "rb")
    pcapheader = 0
    data = f.read(length)
    # print(type(data))
    pm = get_pcapheader_mode()
    # ph = Pcapheader(24)
    msg = read_bin(length, data)
    print(msg[:50])
    (ph, msg) = Matching_message(msg, [pm])
    # msg = msg[24: ]
    adder = 24
    while len(msg) != 0:
        length, msg = read_packetheader(msg)
        # read_packetdata()
        adder += length
        print(length, adder)
        input()
    
    # ph.Print()